package com.gyhoo.firstbootdemo.dao;

import com.gyhoo.firstbootdemo.pojo.Employee;
import org.springframework.stereotype.Repository;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

//员工DAO
@Repository
public class EmployeeDao {
    private static Map<Integer, Employee> employees = null;


    private static DepartmentDao departmentDao = new DepartmentDao();




    static {
        employees = new HashMap<>();

        employees.put(1001,new Employee(1001,"a","a123456@xx.com",1,
                departmentDao.getDepartmentById(101)));
        employees.put(1002,new Employee(1002,"b","b123456@xx.com",0,
                departmentDao.getDepartmentById(102)));
        employees.put(1003,new Employee(1003,"c","c123456@xx.com",1,
                departmentDao.getDepartmentById(103)));
        employees.put(1004,new Employee(1004,"d","d123456@xx.com",1,
                departmentDao.getDepartmentById(104)));
        employees.put(1005,new Employee(1005,"e","e123456@xx.com",0,
                departmentDao.getDepartmentById(105)));
    }

    public Collection getEmployees(){
        return employees.values();
    }
    //查询
    public Employee getEmployeeById(Integer id){
        return employees.get(id);
    }
    //新增员工
    private static int initId = 1006;
    public void insertEmployee(Employee employee){
        //id自增
        if (employee.getId() == null){
            employee.setId(initId++);
        }
        //获取id返回的不能对象，防止篡改
        employee.setDepartment(departmentDao.getDepartmentById(employee.getDepartment().getId()));
        employees.put(employee.getId(),employee);
    }
    //删除员工
    public void deleteEmployee(Integer id){
        employees.remove(id);
    }
    //修改员工
    public void updateEmployee(Employee employee){
        if (employees.get(employee.getId()) != null)
            deleteEmployee(employee.getId());
        insertEmployee(employee);
    }

}
